package com.cqc.paoclient.ui.mime.myarticle;

import com.cqc.paoclient.http.RetrofitHelper;
import com.cqc.paoclient.model.ArticleModel;
import com.cqc.paoclient.model.MyProfileModel;
import com.cqc.paoclient.util.NetErrorUtil;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by cui on 2017/11/26.
 */

public class MyArticlePresenter extends MyArticleContract.Presenter {
    int p = 0;

    @Override
    void getMyArticle() {
        disposable.add(RetrofitHelper.createUserApi().myArticle(p)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ArticleModel>() {
                    @Override
                    public void accept(ArticleModel myProfileModel) throws Exception {
                        mView.getMyArticleSuccess(myProfileModel);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.getMyArticleFail(NetErrorUtil.handleThrowable(throwable));
                    }
                }));
    }

    @Override
    void loadMoreMyArticle() {
        disposable.add(RetrofitHelper.createUserApi().myArticle(++p)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ArticleModel>() {
                    @Override
                    public void accept(ArticleModel articleModel) throws Exception {
                        mView.loadMoreMyArticleSuccess(articleModel);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.getMyArticleFail(NetErrorUtil.handleThrowable(throwable));
                    }
                })
        );
    }
}
